package cn.youyeku.zsxh.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.AppConfig;
import cn.youyeku.zsxh.Utils.CircleImageView;
import cn.youyeku.zsxh.Utils.ImageUtils;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.app;
import cn.youyeku.zsxh.bean.Msg;
import cn.youyeku.zsxh.common.Contanst;
import cn.youyeku.zsxh.service.MessageType;


public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> implements MessageType {
    private List<Msg> mMsgList;

    public ChatAdapter(List<Msg> mMsgList) {
        this.mMsgList = mMsgList;
    }


    //    onCreateViewHolder->viewholder->binddata
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.recycle_chat_item, null);
        LogUtils.w("onCreateViewHolder viewType" + String.valueOf(viewType));
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Msg msg = mMsgList.get(position);
        if (msg.getType() == MESSAGE_TYPE_FRIEND) {
            holder.msg_left_img.setVisibility(View.VISIBLE);
            holder.msg_lright_img.setVisibility(View.GONE);
            holder.tv_receive.setVisibility(View.VISIBLE);
            holder.tv_me.setVisibility(View.GONE);
            holder.tv_receive.setText(msg.getContent());
            holder.chat_time.setVisibility(View.GONE);
            ImageUtils.GlideCircleImg(app.getAppContext(),holder.msg_left_img, XhApi.getFace(Contanst.CHAT_USER_ID));
            LogUtils.w("onBindViewHolder data" + msg.getContent());
        } else {
            holder.msg_lright_img.setVisibility(View.VISIBLE);
            holder.msg_left_img.setVisibility(View.GONE);
            holder.tv_me.setVisibility(View.VISIBLE);
            holder.tv_receive.setVisibility(View.GONE);
            holder.tv_me.setText(msg.getContent());
            holder.chat_time.setText(msg.getTime());
            ImageUtils.GlideCircleImg(app.getAppContext(),holder.msg_lright_img, XhApi.getFace(AppConfig.CONF_USER_ID));
            LogUtils.w("onBindViewHolder data" + msg.getContent());
        }
    }


    public void add(List<Msg> msgs) {
        if (msgs.size() > 0) {
            mMsgList = msgs;
            notifyDataSetChanged();
        }

    }

    @Override
    public int getItemCount() {
        LogUtils.w("getItemCount" + mMsgList.size());
        return mMsgList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_receive;
        private TextView tv_me;
        private CircleImageView msg_left_img;
        private CircleImageView msg_lright_img;
        private TextView chat_time;

        ViewHolder(View itemView) {


            super(itemView);
            tv_receive = (TextView) itemView.findViewById(R.id.tv_friends);
            tv_me = (TextView) itemView.findViewById(R.id.tv_me);
            msg_left_img = (CircleImageView) itemView.findViewById(R.id.msg_left_img);
            msg_lright_img = (CircleImageView) itemView.findViewById(R.id.msg_right_img);
            chat_time = (TextView) itemView.findViewById(R.id.msg_time);

            LogUtils.w("ViewHolder绑定控件");
        }

    }


}